package cn.jslancer.webx.parse;

/**
 * @author zhuangchao
 * @date 2018-07-27 16:23
 */
public class PrimitiveTypeParser implements Parser {
    @Override
    public <T> T parse(String value, Class<T> cls) {
        Object object = null;

        // class 判断包装类型， TYPE 判断基本类型
        if (cls == Integer.class || cls == Integer.TYPE) {
            object = Integer.parseInt(value);
        } else if (cls == Double.class || cls == Double.TYPE) {
            object = Double.parseDouble(value);
        } else if (cls == Float.class || cls == Float.TYPE) {
            object = Float.parseFloat(value);
        } else if (cls == Long.class || cls == Long.TYPE) {
            object = Long.parseLong(value);
        } else if (cls == Byte.class || cls == Byte.TYPE) {
            object = Byte.parseByte(value);
        } else if (cls == Short.class || cls == Short.TYPE) {
            object = Short.parseShort(value);
        }
        return (T) object;
    }

    private boolean isBasicType(Class<?> cls) {
        if (cls == Integer.class || cls == Integer.TYPE
                || cls == Double.class || cls == Double.TYPE
                || cls == Float.class || cls == Float.TYPE
                || cls == Long.class || cls == Long.TYPE
                || cls == Byte.class || cls == Byte.TYPE
                || cls == Short.class || cls == Short.TYPE
                || cls == String.class) {
            return true;
        }

        return false;
    }

    public static void main(String[] args) {
        Parser parser = new PrimitiveTypeParser();
        Integer parse = parser.parse("2", Integer.class);
        System.out.println(parse);
    }
}
